package com.eazy.lkv2.web.controller.hotel;

import com.eazy.lkv2.core.util.QiniuUpload;
import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.ConferenceHall;
import com.eazy.lkv2.web.entity.ConferenceHallImage;
import com.eazy.lkv2.web.entity.Room;
import com.eazy.lkv2.web.entity.RoomImage;
import com.eazy.lkv2.web.service.hotel.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author hpb
 * @date 2016/04/01
 * @desc 会议厅管理
 */
@Controller
@RequestMapping(value = "hall")
public class ConferenceHallController extends BaseController {

	@Autowired
	private ConferenceHallService conferenceHallService;

	@Autowired
	ConferenceHallImageService conferenceHallImageService;

	@Autowired
	HotelService hotelService;

	/**
	 * 查询列表
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView list() {
		List<Map<String,Object>>list=conferenceHallService.select();
		addObject("data", list);
		addObject("count", list.size());
		return getSuccess("hall/hall_list");
	}
	/**
	 * 根据ID删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deletes/{ids}", method = RequestMethod.POST)
	@ResponseBody
	public String deleteHotel(@PathVariable("ids") String[] ids) {
		for (String id : ids) {
			conferenceHallService.removeById(id);
		}
		return "success";
	}
	/**
	 * 跳转到添加页面
	 */
	@RequestMapping(value = "/toAddHall", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toAddHall() {
		return new ModelAndView("/hall/hall_add", "data", hotelService.queryByList(null));
	}


	/**
	 * 添加会议厅
	 */
	@RequestMapping(value = "/addHall", method = { RequestMethod.POST, RequestMethod.GET })
	public void addHall(ConferenceHall hall) {
		String[] title = getParaValues("title");
		String[] success = getParaValues("success");

		if (title == null) {
			renderJsonError("图片不能为空");
			return;
		}
		if (success == null) {
			renderJsonError("请先批量上传图片，然后在提交！！！！");
			return;
		}

		Integer hotel_id = getParaToInt("hotel_id");
		hall.setHotelId(hotel_id);

		String hallId = conferenceHallService.create(hall);

		List<ConferenceHallImage> imageBatch = Lists.newArrayList();
		// 在添加关联表数据
		for (int i = 0; i < title.length; i++) {
			String photo_name = QiniuUpload.IMG_HOST+title[i];
			imageBatch.add(new ConferenceHallImage(photo_name,Integer.parseInt(hallId)));
		}
		renderResult(conferenceHallImageService.createOfBatch(imageBatch));
	}

	/**
	 * 跳转到会议厅图片
	 */
	@RequestMapping(value = "/toHallImage/{id}", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toRoomImage(@PathVariable String id) {
		//FIXME 后续优化
		ConferenceHallImage chImage=new ConferenceHallImage(Integer.parseInt(id));
		addObject("data", conferenceHallImageService.queryByObject(chImage));
		ConferenceHall conferenceHall=conferenceHallService.findById(id);
		addObject("hallName",conferenceHall.getName());
		return getSuccess("hall/hall_image");
	}
}
